package com.good.jzhouse.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.good.jzhouse.bean.*;
import com.good.jzhouse.dao.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("sell")
public class InfoManagementController {
    @Resource
    HouseDao houseDao;
    @Resource
    UsersDao usersDao;
    @Resource
    SellhouseDao sellhouseDao;
    @Resource
    VillageDao villageDao;
    @Resource
    HouseTypeDao houseTypeDao;
    @Resource
    AreaDao areaDao;
    @Resource
    RenovationDao renovationDao;
    @Resource
    FacilitiesDao facilitiesDao;
    @Resource
    LabelDao labelDao;
    @Resource
    LablelHouseDao lablelHouseDao;
    @Resource
    BuyHouseDao buyHouseDao;

    @RequestMapping("/sellinfo")
    @ResponseBody
    public String sellinfo(Integer page,Integer limit,String title,HttpSession session){
        if (limit == null || limit==0)limit = 1;
        if (page == null)page = 1;
        Users user = (Users)session.getAttribute("user");
        Map<String,Object> map = new HashMap<>();
        if(user.getRole().getRoleid()==1){
           map.put("data", sellhouseDao.selectByHouse(title,(page-1)*limit,1,2,limit));
           map.put("count",sellhouseDao.selectByHouseCount(title,1,2));
        }else if(user.getRole().getRoleid()==2){
            map.put("data",JSONObject.toJSON(sellhouseDao.selectByHouseId(title,user.getUid(),1,2,(page-1)*limit,limit)));
            map.put("count",sellhouseDao.selectByHouseCountById(title,user.getUid(),1,2));
        }
        map.put("code",0);
        map.put("msg","成功");

        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
    }

    @RequestMapping("/sellinfo2")
    @ResponseBody
    public String sellinfo2(Integer page,Integer limit,String title,HttpSession session){
        if (limit == null || limit==0)limit = 1;
        if (page == null)page = 1;
        Users user = (Users)session.getAttribute("user");
        Map<String,Object> map = new HashMap<>();
        //roleid==1 为管理员 查询全部
        if(user.getRole().getRoleid()==1){
            map.put("data", sellhouseDao.selectByHouse(title,(page-1)*limit,0,2,limit));
            map.put("count",sellhouseDao.selectByHouseCount(title,0,2));
            //roleid==2 为普通用户 查询自身信息
        }else if(user.getRole().getRoleid()==2){
            map.put("data",JSONObject.toJSON(sellhouseDao.selectByHouseId(title,user.getUid(),0,2,(page-1)*limit,limit)));
            map.put("count",sellhouseDao.selectByHouseCountById(title,user.getUid(),0,2));
        }
        map.put("code",0);
        map.put("msg","成功");

        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
    }


    @RequestMapping("/sellinfo3")
    @ResponseBody
    public String sellinfo3(Integer page,Integer limit,String title,HttpSession session){
        if (limit == null || limit==0)limit = 1;
        if (page == null)page = 1;
        Users user = (Users)session.getAttribute("user");
        Map<String,Object> map = new HashMap<>();
        if(user.getRole().getRoleid()==1){
            map.put("data", sellhouseDao.selectByHouse(title,(page-1)*limit,1,3,limit));
            map.put("count",sellhouseDao.selectByHouseCount(title,1,3));
        }else if(user.getRole().getRoleid()==2){
            map.put("data",JSONObject.toJSON(sellhouseDao.selectByHouseId(title,user.getUid(),1,3,(page-1)*limit,limit)));
            map.put("count",sellhouseDao.selectByHouseCountById(title,user.getUid(),1,3));
        }
        map.put("code",0);
        map.put("msg","成功");

        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
    }

    @RequestMapping("/sellinfo4")
    @ResponseBody
    public String sellinfo4(Integer page,Integer limit,String title,HttpSession session){
        if (limit == null || limit==0)limit = 1;
        if (page == null)page = 1;
        Users user = (Users)session.getAttribute("user");
        Map<String,Object> map = new HashMap<>();
        //roleid==1 为管理员 查询全部
        if(user.getRole().getRoleid()==1){
            map.put("data", sellhouseDao.selectByHouse(title,(page-1)*limit,0,3,limit));
            map.put("count",sellhouseDao.selectByHouseCount(title,0,3));
            //roleid==2 为普通用户 查询自身信息
        }else if(user.getRole().getRoleid()==2){
            map.put("data",JSONObject.toJSON(sellhouseDao.selectByHouseId(title,user.getUid(),0,3,(page-1)*limit,limit)));
            map.put("count",sellhouseDao.selectByHouseCountById(title,user.getUid(),0,3));
        }
        map.put("code",0);
        map.put("msg","成功");

        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
    }

    /******************************************以下是买房租房******************************************/

    @RequestMapping("/buyinfo")
    @ResponseBody
    public String sellinfo5(Integer page,Integer limit,String title,HttpSession session){
        if (limit == null || limit==0)limit = 1;
        if (page == null)page = 1;
        Integer type=1;//租房
        Users user = (Users)session.getAttribute("user");
        Map<String,Object> map = new HashMap<>();
        if(user.getRole().getRoleid()==1){
            map.put("data", buyHouseDao.selectByHouse(type,title,(page-1)*limit,limit));
            map.put("count",buyHouseDao.selectByHouseCount(title,type));
        }else if(user.getRole().getRoleid()==2){
            map.put("data",JSONObject.toJSON(buyHouseDao.selectByHouseId(title,user.getUid(),type,(page-1)*limit,limit)));
            map.put("count",buyHouseDao.selectByHouseCountById(title,user.getUid(),type));
        }
        map.put("code",0);
        map.put("msg","成功");

        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
    }

    @RequestMapping("/buyinfo2")
    @ResponseBody
    public String sellinfo6(Integer page,Integer limit,String title,HttpSession session){
        if (limit == null || limit==0)limit = 1;
        if (page == null)page = 1;
        Integer type=2;//租房
        Users user = (Users)session.getAttribute("user");
        Map<String,Object> map = new HashMap<>();
        if(user.getRole().getRoleid()==1){
            map.put("data", buyHouseDao.selectByHouse(type,title,(page-1)*limit,limit));
            map.put("count",buyHouseDao.selectByHouseCount(title,type));
        }else if(user.getRole().getRoleid()==2){
            map.put("data",JSONObject.toJSON(buyHouseDao.selectByHouseId(title,user.getUid(),type,(page-1)*limit,limit)));
            map.put("count",buyHouseDao.selectByHouseCountById(title,user.getUid(),type));
        }
        map.put("code",0);
        map.put("msg","成功");

        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
    }



    @RequestMapping("/status2")
    @ResponseBody
    public Boolean status2(Integer downid,Integer upid,Integer id,Integer buyid){
        if(downid!=null && sellhouseDao.changeonlinedown(downid)>0){
            return true;
        }
        if(upid!=null && sellhouseDao.changeonlineup(upid)>0){
            return true;
        };
        if(id!=null && sellhouseDao.del(id)>0){
            return true;
        };
        if(buyid!=null && buyHouseDao.delById(buyid)>0){
            return true;
        };
        return false;
    }


    @RequestMapping("/modify")
    public String modify (Model model,HttpSession s, Integer id,HttpServletResponse response) throws IOException {
        s.setAttribute ("village",villageDao.findAll());
        s.setAttribute ("housetype",houseTypeDao.findAll());
        s.setAttribute ("area",areaDao.findAll());
        s.setAttribute ("renovation",renovationDao.findAll());
        s.setAttribute ("facilities",facilitiesDao.findAll());
        s.setAttribute ("label",labelDao.selectType1());
        model.addAttribute("house",houseDao.selectById(id));
       return "HouseForSaleEdit";
    }

    @RequestMapping("/modify2")
    public String modify2 (Model model,HttpSession s, Integer id,HttpServletResponse response) throws IOException {
        s.setAttribute ("village",villageDao.findAll());
        s.setAttribute ("housetype",houseTypeDao.findAll());
        s.setAttribute ("area",areaDao.findAll());
        s.setAttribute ("renovation",renovationDao.findAll());
        s.setAttribute ("facilities",facilitiesDao.findAll());
        s.setAttribute ("label",labelDao.selectType1());
        model.addAttribute("house",houseDao.selectById(id));
        return "HouseRentalEdit";
    }
    

    @RequestMapping("update")
    @ResponseBody
    public boolean update(House house,Integer etime,int[] names){
        Sellhouse sellhouse = sellhouseDao.selects(house.getId());
        sellhouse.setBeginTime(new Date());
        sellhouse.setEffectiveTime(etime);
        Date d = new Date();
        Timestamp t = new Timestamp(d.getTime());
        house.setBeginTime(t);
        if(houseDao.update(house)>0){
            if (names!=null){
                lablelHouseDao.delhouselabel(house.getId());
                for (int i=0;i < names.length;i++){
                    LabelHouse lh = new LabelHouse(null,house,new Label(names[i]));
                    lablelHouseDao.insert(lh);
                }
            }
            if(sellhouseDao.update(sellhouse)>0){
                return true;
            }
        }
        return false;
    }
}
